package com.goods.business.service.imp;

import com.goods.business.mapper.InStockInfoMapper;
import com.goods.business.mapper.InStockMapper;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.InStockService;
import com.goods.common.model.business.InStock;
import com.goods.common.model.business.InStockInfo;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.Supplier;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.InStockDetailVO;
import com.goods.common.vo.business.InStockItemVO;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class InStockServiceImpl implements InStockService {

    @Autowired
    private InStockMapper inStockMapper;
    @Autowired
    private InStockInfoMapper inStockInfoMapper;
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private ProductMapper productMapper;

    //查询入库纪录 带条件查询带分页
    @Override
    public PageVO<InStockVO> findInStockList(Integer pageNum, Integer pageSize, InStockVO inStockVOArgs) {
        List<InStock> inStockList = inStockMapper.findInStockList(inStockVOArgs.getStatus(), inStockVOArgs.getType(), inStockVOArgs.getInNum(), inStockVOArgs.getStartTime(), inStockVOArgs.getEndTime());
        List<InStockVO> inStockVOList = inStockList.stream().map(inStock -> {
            InStockVO inStockVO = new InStockVO();
            BeanUtils.copyProperties(inStock, inStockVO);
            Supplier supplier = supplierMapper.selectByPrimaryKey(inStock.getSupplierId());
            inStockVO.setPhone(supplier.getPhone());
            inStockVO.setSupplierName(supplier.getName());
            return inStockVO;
        }).collect(Collectors.toList());
        List<InStockVO> inStockVOListPage = ListPageUtils.page(inStockVOList, pageSize, pageNum);
        return new PageVO<InStockVO>(inStockVOList.size(), inStockVOListPage);
    }

    //查看入库单明细
    @Override
    public InStockDetailVO detail(Long id, Long pageNum) {
        InStockDetailVO inStockDetailVO = new InStockDetailVO();
        //InStockDetailVO的一个属性
        List<InStockItemVO> itemVOS = new ArrayList<>();
        //查询入库纪录
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        //复制
        BeanUtils.copyProperties(inStock, inStockDetailVO);
        inStockDetailVO.setTotal(inStock.getProductNumber());
        //根据入库单编号查询记录中多条商品的信息
        InStockInfo inStockInfo = new InStockInfo();
        inStockInfo.setInNum(inStock.getInNum());
        List<InStockInfo> inStockInfoList = inStockInfoMapper.select(inStockInfo);
        //根据商品编号查询商品
        for (InStockInfo stockInfo : inStockInfoList) {
            InStockItemVO inStockItemVO = new InStockItemVO();
            Product product = new Product();
            product.setPNum(stockInfo.getPNum());
            product = productMapper.selectOne(product);
            BeanUtils.copyProperties(product, inStockItemVO);
            inStockItemVO.setCount(stockInfo.getProductNumber());
            itemVOS.add(inStockItemVO);
        }
        inStockDetailVO.setItemVOS(itemVOS);
        //根据入库纪录查询来源地址
        Supplier supplier = supplierMapper.selectByPrimaryKey(inStock.getSupplierId());
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier, supplierVO);
        inStockDetailVO.setSupplierVO(supplierVO);
        return inStockDetailVO;
    }

    //入库
    @Override
    public void addIntoStock(Map addRuleForm) {
        Long supplierId = (Long) addRuleForm.get("supplierId");
        Supplier supplier = new Supplier();//为了主键回显并设置
        //先生成物资提供方信息
        if (null == supplierId) {
            //说明来源是新增的
            //添加操作无需时间格式转换
            try {
                org.apache.commons.beanutils.BeanUtils.populate(supplier, addRuleForm);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            supplier.setCreateTime(new Date());
            supplier.setModifiedTime(new Date());
            //id类型为bigint 即 Long 不转也行 自动装箱
            supplierMapper.insert(supplier);
        }
        String inNum = UUID.randomUUID().toString().replaceAll("-", "");
        InStock inStock = new InStock();
        inStock.setInNum(inNum);
        inStock.setType((Integer) addRuleForm.get("type"));//类型
        inStock.setOperator("admin");
        inStock.setCreateTime(new Date());
        inStock.setModified(new Date());
        inStock.setSupplierId(supplier.getId());
        inStock.setRemark((String) addRuleForm.get("remark"));//描述
        inStock.setStatus(2);

        List<Map> productList = (List<Map>) addRuleForm.get("products");//productId productNumber
        //为了设置入库纪录的总数量
        int productNumberCount = 0;
        InStockInfo inStockInfo = new InStockInfo();
        //遍历集合 添加到入库详情表 每条信息是一种商品
        for (int i = 0; i < productList.size(); i++) {
            Map productMap = productList.get(i);
            //根据productId查询商品
            Product product = productMapper.selectByPrimaryKey(productMap.get("productId"));
            inStockInfo.setInNum(inNum);
            inStockInfo.setPNum(product.getPNum());
            Integer productNumber = (Integer) productMap.get("productNumber");
            inStockInfo.setProductNumber(productNumber);
            inStockInfo.setCreateTime(new Date());
            inStockInfo.setModifiedTime(new Date());
            inStockInfoMapper.insert(inStockInfo);
            productNumberCount = productNumber + productNumber;
        }
        inStock.setProductNumber(productNumberCount);
        inStockMapper.insert(inStock);
    }

    @Override
    public void remove(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        inStock.setStatus(1);
        inStockMapper.updateByPrimaryKeySelective(inStock);
    }

    @Override
    public void back(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        inStock.setStatus(0);
        inStockMapper.updateByPrimaryKeySelective(inStock);
    }

    //审核入库纪录通过
    @Override
    public void publish(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        inStock.setStatus(0);
        inStockMapper.updateByPrimaryKeySelective(inStock);
        //TODO 修改库存
//        InStockInfo inStockInfo = new InStockInfo();
//        inStockInfo.setInNum(inStock.getInNum());
//        List<InStockInfo> inStockInfoList = inStockInfoMapper.select(inStockInfo);
//        for (InStockInfo stockInfo : inStockInfoList) {
//
//        }
    }

    //删除入库纪录 没必要减库存
    @Override
    public void delete(Long id) {
        inStockMapper.deleteByPrimaryKey(id);
    }
}

